<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Models\Admin\Node;

class NodeController extends Controller
{
    public function index(Request $request) {
		if($request->isMethod('post')){
			$nodes = Node::get()->toArray();
			$common = new Common();
			$tree = $common->getTree($nodes,false);
			return ['code'=>1,'data'=>$tree,'msg'=>'ok'];
		}
		return view('admin.node.index');
	}

	public function nodeAdd(Request $request) {
		if($request->isMethod('post')){
			$param = $_POST;
			unset($param['_token']);
			$param['src'] = mb_strtolower($param['ctrl'])."/".mb_strtolower($param['action']);
			$param['ctrl'] = ucfirst($param['ctrl']);
			$res = Node::insert($param);
			if($res !== false){
				return ['code'=>1,'msg'=>'添加成功'];
			}else{
				return ['code'=>0,'msg'=>'添加失败'];
			}
		}
	}

	public function nodeEdit(Request $request){
		if($request->isMethod('post')){
			$param = $_POST;
			unset($param['_token']);
			$param['src'] = mb_strtolower($param['ctrl'])."/".mb_strtolower($param['action']);
			$param['ctrl'] = ucfirst($param['ctrl']);
			$res = Node::where('id',$param['id'])->update($param);
			if($res !== false){
				return ['code'=>1,'msg'=>'编辑成功'];
			}else{
				return ['code'=>0,'msg'=>'编辑失败'];
			}
		}
	}
	public function nodeDel(Request $request){
		if($request->isMethod('post')){
			$param = $_POST;
			$res = Node::where('id',$param['id'])->delete();
			if($res !== false){
				return ['code'=>1,'msg'=>'删除成功'];
			}else{
				return ['code'=>0,'msg'=>'删除失败'];
			}
		}
	}
}
